package com.gugee.tiktok.common.model.dal.challenge;

import com.gugee.tiktok.common.model.mysql.ChallengeRecord;
import com.gugee.tiktok.common.model.ranking.BloggerGrowth;
import com.gugee.tiktok.common.model.ranking.GrowthData;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface ChallengeRecordRepository extends CrudRepository<ChallengeRecord,Long> {

    Optional<ChallengeRecord> findByCidAndDateCode(long cid, int dateCode);

    List<ChallengeRecord> findByCidAndDateCodeBetweenOrderByDateCode(long cid, long dateCodeStart, int dateCodeEnd);


    @Query("SELECT " +
            "IFNULL(ROUND(AVG(user_growth)),0) user_growth, IFNULL(ROUND(SUM(view_growth)),0) view_growth " +
            "FROM challenge_record where cid = :cid and user_growth >0 and date_code BETWEEN :startDateCode AND :endDateCode")
    GrowthData findByGrowthData(long cid, int startDateCode, int endDateCode);
}
